﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;

namespace ZLCMS.Web.SystemManage.Basics.Location
{
    public partial class LocationDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //todo 设置页面控件信息

                int uid = 0;
                try
                {
                    uid = Int32.Parse(Request["uid"]);
                }
                catch
                {

                }
                DataAddressDesc item = null;
                using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    item = session.Get<DataAddressDesc>(uid);
                    if (item != null)
                    {
                        //保存uid
                        ViewState["uid"] = uid;
                    }
                    else
                    {
                        item = new DataAddressDesc();
                    }
                }
                
                //在此设置控件的初始属性
                ProvinceCascadeCombox1.SelectedValue = item.ProviceCode;
                CityCascadeCombox1.SelectedValue = item.CitypCode;
                txtSummary.Value = item.Description;

            }
            else
            {
                //todo 保存数据
                
                int uid = ViewState["uid"] == null ? 0 : (int)ViewState["uid"];
                DataAddressDesc item = null;
                using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    item = session.Get<DataAddressDesc>(uid);
                    if (item == null)
                    {
                        item = new DataAddressDesc();
                    }

                    //在此设置item的各个属性
                    item.ProviceCode = ProvinceCascadeCombox1.SelectedValue;
                    item.CitypCode = CityCascadeCombox1.SelectedValue;
                    item.Description = txtSummary.Value;
                    //保存并更新item
                    session.SaveOrUpdate(item);
                    session.Flush();
                }


                //框架返回成功提示
                ZLCMS.Business.DwzFrame.FrameHelper _framePage = new ZLCMS.Business.DwzFrame.FrameHelper(this);
                ZLCMS.Business.DwzFrame.ReturnCode rc = new ZLCMS.Business.DwzFrame.ReturnCode()
                {
                    statusCode = 200,
                    message = "操作成功"
                };

                rc.callbackType = "forward";
                rc.forwardUrl = "/SystemManage/Basics/Location/LocationDetail.aspx?tabid=LocationDetail";

                if (Request["submit_op"] != "1")
                {
                    rc.forwardUrl += "&uid=" + item.Id;
                }

                _framePage.Alert(rc);
            }
        }
    }
}